<template>
    <div class="willLogin">
        <div class="header">
            <span @click="GoBack()"><van-icon name="arrow-left" /></span>
            <p>注册</p>
            <span></span>
        </div>
        <section>
            <div class="loginInputs">
                <input type="text" placeholder="请输入验证码" v-model="changeCode">
                <div class="openSwitch" @click="initChangeCode()">
                    <img :src="configUrl" alt="">
                </div>
            </div>
            <div class="loginInputs">
                <input type="text" placeholder="请输入手机号" v-model="cellphone">
                <div class="openSwitch">
                    <button @click="getCode()">获取验证码</button>
                </div>
            </div>
            <div class="loginInputs">
                <input type="text" placeholder="请输入验证码" v-model="messageCode">
                <div class="openSwitch">

                </div>
            </div>
            <div class="loginInputs">
                <input type="password" class="pwd" placeholder="请输入密码" v-model="pwd">
                <div class="openSwitch">
                    <van-switch v-model="checked" @change="changeInput()" active-color="#07c160" inactive-color="white" size="26px"/>
                </div>
            </div>
            <button class="doLogin" @click="login()">立即注册</button>
        </section>
    </div>
</template>
<script>
    import {RegisterUser,IsRegister} from "../../api/list/list";

    export default {
        data(){
            return{
                checked:false,
                cellphone:'',
                pwd:'',
                configUrl:'',
                changeCode:'',
                messageCode:'',
                changeCodes:''
            }
        },
        created(){
          this.initChangeCode()
        },
        methods:{
            changeInput(){
                let pwd = document.getElementsByClassName('pwd')[0]
                if(this.checked){
                    pwd.type = 'text'
                }else{
                    pwd.type = 'password'
                }
            },
            getCode(){
              if(/^1[3456789]\d{9}$/.test(this.cellphone)){
                  let str = ''
                  for(let i=0;i<4;i++){
                      str += Math.floor(Math.random()*9)
                  }
                  this.changeCodes = str
                  alert(str)
              }else{
                  this.$toast.fail('手机号输入不正确')
              }
            },
            login(){
                if(this.changeCode == ''){
                    this.$toast.fail('您还未输入验证码')
                }else if(this.cellphone == '' || !(/^1[3456789]\d{9}$/.test(this.cellphone))){
                    this.$toast.fail('手机号输入不正确')
                }else if(this.messageCode != this.changeCodes){
                    this.$toast.fail('您输入的短信验证码不正确')
                }else if(this.pwd == ''){
                    this.$toast.fail('您还未输入密码')
                }else{
                    const userId = {
                        username:this.cellphone
                    }
                    IsRegister(userId).then((res)=>{
                        console.log(res.data.data.isreg)
                        if(res.data.data.isreg == 1){
                            this.$toast.fail('您已经注册过账户')
                        }else{
                            const params = {
                                cellphone : this.cellphone,
                                password : this.pwd
                            }
                            RegisterUser(params).then((res)=>{
                                console.log(res.data.code)
                                if(res.data.code == 200){
                                    this.$toast.success('注册成功');
                                    this.$router.replace('/login')
                                }
                            })
                        }
                    })

                }

            },
            GoBack(){
                history.go(-1)
            },
            quickRegister(){
                this.$router.push('/willLogin')
            },
            initChangeCode(){
                let url = 'http://vueshop.glbuys.com/api/vcode/chkcode?token=1ec949a15fb709370f'
                let num = Math.floor(Math.random()*3)
                this.configUrl = url +"&index="+num
            }
        }
    }
</script>
<style lang="less" scoped>
    .willLogin{
        width: 100%;
        height: 100%;
        display: flex;
        flex-direction: column;
        .header{
            width: 100%;
            height: 1rem;
            display: flex;
            border-bottom: 1px solid #efefef;
            background: white;
            span{
                width: 1rem;
                display: flex;
                align-items: center;
                justify-content: center;
                font-size: 0.4rem;
            }
            p{
                flex: 1;
                text-align: center;
                line-height: 1rem;
                font-size: 0.32rem;
            }
        }
        section{
            flex: 1;
            padding: 0.1rem 0.2rem;
            box-sizing: border-box;
            .loginInputs{
                border: 1px solid #efefef;
                display: flex;
                border-radius: 2px;
                margin: 0.2rem 0;
                input{
                    padding-left: 0.2rem;
                    box-sizing: border-box;
                    flex: 1;
                    height: 0.8rem;
                }
                .openSwitch{
                    width: 1.5rem;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                }
            }
            .doLogin{
                background: #EB1625;
                color: white;
                border-radius: 4px;
                width: 100%;
                height: 0.8rem;
                border: none;
                font-size: 0.28rem;
            }
            .doMore{
                font-size: 0.2rem;
                height: 0.6rem;
                display: flex;
                justify-content: space-between;
                align-items: center;
            }
        }
    }
</style>